﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using CentralOffice.Archive.Data;
using CentralOffice.Archive.BusinessActions;

using PublicDatumTable = CentralOffice.Archive.Data.PublicDatumDataSet.PublicDatumDataTable;
using PublicDatumRow = CentralOffice.Archive.Data.PublicDatumDataSet.PublicDatumRow;


namespace CentralOffice.WebClient.Archives
{
    public partial class PublicDatumEdit : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                SiteUtil.PopulateListBox(ddlCategory, SiteConstants.ArchiveType, false);
                ddlCategory.SelectedIndex = 0;

                string archiveId = HttpContext.Current.Request.QueryString["id"];
                if (String.IsNullOrEmpty(archiveId))
                {
                    //txtFiledDate.Text = DateTime.Now.ToString();
                    //txtFiledDate.Enabled = false;

                    //txtOrderNumber.Text = (Archive.BusinessActions.Archive.GetMaxOrderNumber() + 1).ToString();
                }
                else
                {
                    SetFields(archiveId);
                }
            }

        }


        private void SetFields(string id)
        {
            if (String.IsNullOrEmpty(id))
                return;
            PublicDatumRow publicDatum = PublicDatum.GetById(new Guid(id));

            txtName.Text = publicDatum.Name;

            if (!publicDatum.IsCategoryNull())
            {
                ddlCategory.SelectedValue = publicDatum.Category.ToString(); 
            }
            txtRemark.Text = publicDatum.IsRemarkNull() ? "" : publicDatum.Remark;
        }

        private PublicDatumDataSet GetFields(string id)
        {
            PublicDatumDataSet publicDatumDataSet = null;
            PublicDatumRow publicDatum = null;

            if (String.IsNullOrEmpty(id))
            {
                publicDatumDataSet = new PublicDatumDataSet();
                publicDatum = publicDatumDataSet.PublicDatum.NewPublicDatumRow();
                SiteUtil.PopulateCommonField(publicDatum, Guid.NewGuid());
            }
            else
            {
                publicDatum = PublicDatum.GetById(new Guid(id));
                publicDatumDataSet = publicDatum.Table.DataSet as PublicDatumDataSet;
            }

            publicDatum.Name = txtName.Text;
            publicDatum.Category = Int32.Parse(ddlCategory.SelectedValue);
            publicDatum.Remark = txtRemark.Text;
            publicDatum.UploadDate = DateTime.Now;

            string fileName = SaveFile(SiteUtil.GetTaskFilesPath(), fileData);
            if (fileName.Length > 0)
            {
                publicDatum.Attachments = fileName;
            }

            if (String.IsNullOrEmpty(id))
            {
                publicDatum.Table.Rows.Add(publicDatum);
                //document.Table.Rows.Add(document);
            }

            return publicDatumDataSet;
        }

        private string SaveFile(string rootDir, FileUpload file)
        {
            string fileName = "";

            string path = Server.MapPath(rootDir);
            if (file.HasFile)
            {
                //file.SaveAs(path + file.FileName);
                //fileName = rootDir + file.FileName;
                var fn = SiteUtil.GetUploadFileName(path, file.FileName);
                file.SaveAs(fn);
                fileName = rootDir + fn.Substring(fn.LastIndexOf('\\') + 1);
            }
            else
            {

            }
            return fileName;
        }


        private void ClearFields()
        {
            txtName.Text = "";
            txtRemark.Text = "";
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Guid id = this.Save();
            ClearFields();
            Response.Redirect("PublicDatumQuery.aspx?id=" + id);
        }

        protected void btnContinue_Click(object sender, EventArgs e)
        {
            this.Save();
            ClearFields();
        }

        private Guid Save()
        {
            string id = HttpContext.Current.Request.QueryString["id"];

            PublicDatumDataSet publicDatumDataSet = GetFields(id);
            PublicDatum.Update(publicDatumDataSet);

            if (String.IsNullOrEmpty(id))
            {
                id = (publicDatumDataSet.PublicDatum.Rows[0] as PublicDatumRow).Id.ToString();
            }

            return new Guid( id);
        }
    }
}